package com.lhj.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lhj.reggie.common.R;
import com.lhj.reggie.dto.DishDto;
import com.lhj.reggie.entity.Category;
import com.lhj.reggie.entity.Dish;
import com.lhj.reggie.entity.DishFlavor;
import com.lhj.reggie.mapper.CategoryMapper;
import com.lhj.reggie.mapper.DishFlavorMapper;
import com.lhj.reggie.mapper.DishMapper;
import com.lhj.reggie.service.DishFlavorService;
import com.lhj.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.stream.Collectors;


@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Resource
    private DishMapper dishMapper;

    @Resource
    private DishFlavorMapper dishFlavorMapper;

    @Resource
    private DishFlavorService dishFlavorService;

    @Resource
    private CategoryMapper categoryMapper;

    @Transactional
    public R<String> savewithFlavor(DishDto dishDto) {
        //保存菜品信息到菜品表
        dishMapper.insert(dishDto);
        //获取菜品id
        Long dishId = dishDto.getId();
        //获取口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();
        //循环遍历口味赋值
        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishId);
            return item;
        }).collect(Collectors.toList());
        //保存菜品口味信息到菜品口味表
        dishFlavorService.saveBatch(flavors);
        return R.success("新增菜品成功");
    }


    public R<Page> page(int page, int pageSize, String name) {
        //创建分页构造器
        Page<Dish> page1 = new Page<>(page, pageSize);
        Page<DishDto> dishDtoPage = new Page<>(page, pageSize);
        //创建条件构造器
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        //添加条件
        lqw.like(name != null, Dish::getName, name);
        lqw.orderByDesc(Dish::getUpdateTime);
        //执行分页查询
        dishMapper.selectPage(page1, lqw);
        //将page1中的信息拷贝到dishDtoPage
        BeanUtils.copyProperties(page1, dishDtoPage, "records");
        //获取列表数据
        List<Dish> records = page1.getRecords();

        List<DishDto> list = records.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            //将dish的信息拷贝到dishDto
            BeanUtils.copyProperties(item, dishDto);

            Long categoryId = item.getCategoryId();

            Category category = categoryMapper.selectById(categoryId);
            if(category!=null) {
                String categoryName = category.getName();
                dishDto.setCategoryName(categoryName);
            }
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return R.success(dishDtoPage);
    }

    @Transactional
    public DishDto getbyidWithFlavor(Long id) {
        //查询菜品信息
        Dish dish = dishMapper.selectById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        //创建条件构造器
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        //查询当前菜品对应的口味信息
        lqw.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorMapper.selectList(lqw);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Transactional
    public R<String> updatewithFlavor(DishDto dishDto) {
        //更新菜品信息
        dishMapper.updateById(dishDto);
        //清除当前菜品的口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        dishFlavorService.remove(lqw);
        //添加当前提交的过来的口味信息
        List<DishFlavor> flavors = dishDto.getFlavors();

        flavors = flavors.stream().map((item) -> {
            item.setDishId(dishDto.getId());
            return item;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(flavors);
        return R.success("修改菜品成功");
    }

    public R<List<DishDto>> list(Dish dish) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId());
        lqw.eq(Dish::getStatus, 1);
        List<Dish> list = dishMapper.selectList(lqw);
        List<DishDto> dishDtoList =list.stream().map((item) -> {
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryMapper.selectById(categoryId);
            if(category!=null) {
                String name = category.getName();
                dishDto.setCategoryName(name);
            }
            //当前菜品id
            Long dishId = item.getId();
            LambdaQueryWrapper<DishFlavor> lqw1 = new LambdaQueryWrapper<>();
            lqw1.eq(DishFlavor::getDishId,dishId);
            List<DishFlavor> flavors = dishFlavorMapper.selectList(lqw1);
            dishDto.setFlavors(flavors);
            return dishDto;
        }).collect(Collectors.toList());
        return R.success(dishDtoList);
    }
}
